

package vo;

import java.util.ArrayList;
import java.util.Date;
import util.EnumVerdicts;

/**
 *
 * @author sbaldrich
 */
public class VOScoreboard implements Comparable{

    
    class ProblemAttemp
    {
        private int attemps;
        private Date time;
        private boolean accepted;

        public boolean isAccepted() {
            return accepted;
        }

        public void setAccepted(boolean accepted) {
            this.accepted = accepted;
        }

        public int getAttemps() {
            return attemps;
        }

        public void setAttemps(int attemps) {
            this.attemps = attemps;
        }

        public Date getTime() {
            return time;
        }

        public void setTime(Date time) {
            this.time = time;
        }
    }

    public VOScoreboard()
    {
        this.handle = "null";
        this.problems = null;
        this.solved = 0;
        this.time = 0;
    }

    public VOScoreboard(String handle , int nproblems) {
        this.handle = handle;
        this.problems = new ArrayList<ProblemAttemp>(nproblems);
        for(int i=0; i<nproblems; i++)
            problems.add(new ProblemAttemp());
        this.solved = 0;
        this.time = 0;
    }


    public String getHandle() {
        return handle;
    }

    public void setHandle(String handle) {
        this.handle = handle;
    }

    private String handle;
    private ArrayList<ProblemAttemp> problems;
    private int solved;
    private int time;
    private boolean active;

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public ArrayList<ProblemAttemp> getProblems() {
        return problems;
    }

    public void setProblems(ArrayList<ProblemAttemp> problems) {
        this.problems = problems;
    }

    public int getSolved() {
        return solved;
    }

    public void setSolved(int solved) {
        this.solved = solved;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public void processSubmission(VOSubmission vo , int problem)
    {
        setActive(true);
        if(vo.getVerdict().compareTo(EnumVerdicts.CE.value())==0)
            return;
        if(problems.get(problem).isAccepted())
            return;
        if(vo.getVerdict().compareTo(EnumVerdicts.AC.value())==0)
        {
            problems.get(problem).setAccepted(true);
            problems.get(problem).setTime(vo.getDate());
            setTime(getTime() + problems.get(problem).getAttemps());
            setSolved(getSolved()+1);
        }
        problems.get(problem).setAttemps(problems.get(problem).getAttemps()+1);
    }

    @Override
    public String toString()
    {
        return (getHandle() + ":" + getSolved() + "/" +getTime());
    }

    @Override
    public int compareTo(Object other)
    {
        if(this.getSolved() > ((VOScoreboard)(other)).getSolved())
            return 1;
        if(this.getSolved() < ((VOScoreboard)(other)).getSolved())
            return -1;
        if(this.getTime() < ((VOScoreboard)(other)).getTime())
            return 1;
        if(this.getTime() > ((VOScoreboard)(other)).getTime())
            return -1;
        return this.getHandle().compareTo(((VOScoreboard)(other)).getHandle());
    }

}
